$(function() {
	new Vue({
		el: '#addHomeBgApp',
		data: {
			fullscreenLoading: false,
			uploadUrl: "http://39.100.131.147:8888/goods/address/uploadFile",
			form:{
				photo:"",
				showOrder:"",
			},
			fileAvatarList:[],
			carouseImgUrl:[],
			homeBgTable:[],
			shopValue:"",
			options:[],
			dialogEditFormVisible:false,
			formLabelWidth:"120px",
			editFrom:{
				shopName:"",
				showOrder:"",
				photo:"",
				id:"",
			},
			editShopValue:"",
			
		},
		created: function() {
			parent.vueData.$data.hreHtml = "../addHomeBg/index.html";
			parent.vueData.activeIndex = "3";
			parent.vueData.getLeftNav("3")
			parent.vueData.$data.categoryIndex = 6;
			this.getAllHomeBg()
			this.getShopList()
		},
		methods: {
			handleImgSuccess(res, file) {
				 if(res.code == 100){
					 this.form.photo = res.result
					 let obj = new Object();
					 obj.url =  res.result;
					 obj.name = "首页背景图";
					 this.fileAvatarList.push(obj)
				 }
			},
			beforeImgRemove(file,fileList) {
				
				  let index = this.fileAvatarList.findIndex( fileItem => {
				       return fileItem.url === file.url
					})
				  this.fileAvatarList.splice(index,1)
				  return true;
			},
			handleImgExceed(files, fileList) {
				this.$message.warning("首页背景图只能上传一张图片")
			},
			beforeCourseRemove(file, fileList) {
				  let index = this.carouseImgUrl.findIndex( fileItem => {
				       return fileItem.url === file.url
				   })
				  this.carouseImgUrl.splice(index, 1)
				return true;
			},
			handleCoverExceed(files, fileList) {
				this.$message.warning("背景图只能上传一张图片")
			},
			handleCoverSuccess(response, file, fileList) {
				if (response.code == 100) {
					this.editFrom.photo  = response.result
					this.carouseImgUrl.push({
						name: "logo",
			            url: response.result,
					})
					
				}
			},
			
			onClickAddShopBg(){
				if(this.fileAvatarList.length<=0){
					this.$message.warning("请先选择首页背景图");
					return;
				}
				if(stringEmpty(this.form.showOrder)){
					this.$message.warning("权重不能为空");
					return;
				}
				if (stringEmpty(this.shopValue)){
					this.$message.warning("请选择店铺");
					return;
				}
				let param = {
					photo:this.form.photo,
					shopId:this.shopValue,
					showOrder:this.form.showOrder
				}
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.uploadAdvertisement,
					method: 'post',
					data: param
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.form = {}
						this.shopValue = "",
						this.fileAvatarList = [],
						 this.getAllHomeBg();
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			getAllHomeBg(){
                this.fullscreenLoading = true;
                axios({
                	url: POSTURL.selectAllAdvertisement,
                	method: 'post',
                	data: {}
                }).then(response => {
                	this.fullscreenLoading = false;
                	if (response.data.code === 100) {
                		console.log(response.data);
                		this.homeBgTable  = response.data.result
                	} else {
                		this.$message.error(response.data.message);
                	}
                }).catch(error => {
                	this.fullscreenLoading = false;
                	console.log(error);
                	this.$message.error('服务器出错!');
                })
			},
			getShopList(){
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.selectAllShop,
					method: 'post',
					data: {}
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						console.log(response.data);
						 this.shopList  = response.data.result;
						 for (let item of response.data.result) {
							
						 	  let obj  = new Object();
							  obj.value = item.uid;
							  obj.label = item.name;
							  this.options.push(obj)
						 }
						 console.log(this.options);
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			deleteHomeBgRow(val){
				console.log(val);
				 this.fullscreenLoading = true;
				 axios({
				 	url: POSTURL.deleteAdvertisement,
				 	method: 'post',
				 	data: {
						id:val.id
					}
				 }).then(response => {
				 	this.fullscreenLoading = false;
				 	if (response.data.code === 100) {
				 		   this.getAllHomeBg()
				 	} else {
				 		this.$message.error(response.data.message);
				 	}
				 }).catch(error => {
				 	this.fullscreenLoading = false;
				 	console.log(error);
				 	this.$message.error('服务器出错!');
				 })
			},
			editHomeBgRow(val){
				 this.dialogEditFormVisible = true
				 this.carouseImgUrl= [];
				 this.carouseImgUrl.push({
				 	name: "背景图",
				    url: val.photo,
				 })
				 this.editFrom.shopName = val.shopName
				 this.editFrom.showOrder = val.showOrder
				 this.editFrom.id = val.id
				 this.editFrom.photo = val.photo
				 this.editShopValue  = val.shopId
				 
			},
			configEditUpload(){
                  if(this.carouseImgUrl.length<=0){
                  	this.$message.warning("请先选择首页背景图");
                  	return;
                  }
                  if(stringEmpty(this.editFrom.showOrder)){
                  	this.$message.warning("权重不能为空");
                  	return;
                  }
                  if (stringEmpty(this.editShopValue)){
                  	this.$message.warning("请选择店铺");
                  	return;
                  }
				  let param = {
					oldId:this.editFrom.id,
				  	photo:this.editFrom.photo,
				  	shopId:this.editShopValue,
				  	showOrder:this.editFrom.showOrder
				  }
				  this.fullscreenLoading = true;
				  axios({
				  	url: POSTURL.uploadAdvertisement,
				  	method: 'post',
				  	data: param
				  }).then(response => {
				  	this.fullscreenLoading = false;
				  	if (response.data.code === 100) {
				  		this.editFrom = {}
				  		this.editShopValue = ""
				  		this.carouseImgUrl = []
						this.dialogEditFormVisible = false
				  		this.getAllHomeBg();
				  	} else {
				  		this.$message.error(response.data.message);
				  	}
				  }).catch(error => {
				  	this.fullscreenLoading = false;
				  	console.log(error);
				  	this.$message.error('服务器出错!');
				  })
				  
			}
			
			
		}	
	})
})
